﻿using Mapster;
using Zhp.Common.WebApp;
using Zhp.Entity.Chat;
using Zhp.Entity.Sys;
using Zhp.Entity.SysEnum;
using Zhp.IService.Chat;
using Zhp.IService.Common;
using Zhp.Model.Chat;
using Zhp.Model.Common;
using Zhp.Service.Base;
using Zhp.SqlSugar.Repository;

namespace Zhp.Service.Chat
{
    public class ChatUserService : BaseService<ChatUser>, IChatUserService
    {
        IBaseRepository<SysUser> _sysUserRep;
        IBaseRepository<ChatFriend> _chatFriendRep;
        IFileService _fileService;
        public ChatUserService(ICurrentUser currentUser, IBaseRepository<ChatUser> rep, IBaseRepository<SysUser> sysUserRep, IBaseRepository<ChatFriend> chatFriendRep, IFileService fileService)
        {
            _currentUser = currentUser;
            _rep = rep;
            _sysUserRep = sysUserRep;
            _chatFriendRep = chatFriendRep;
            _fileService = fileService;
        }

        public async Task<List<ChatUserModel>> GetChatUserList()
        {
            var list = await _rep.GetListAsync(x => x.IsHide == false && x.UserId == _currentUser.UserId);
            var rlist = list.Adapt<List<ChatUserModel>>();
            foreach (var item in rlist)
            {
                item.ToUserAvatar = await _fileService.GetBase64ImageString(FileEnum.Avatar, item.TouserId);
            }
            return rlist;
        }

        public async Task<bool> Hide(long Id)
        {
            var chatUser = await _rep.GetByIdAsync(Id);
            if(chatUser != null)
            {
                chatUser.IsHide = true;
                return await _rep.UpdateAsync(chatUser);
            }
            return false;
        }

        public async Task<bool> Delete(long id)
        {
            return await _rep.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 返回聊天好友对象
        /// </summary>
        /// <param name="friendId"></param>
        /// <returns></returns>
        public async Task<ObjectRet<ChatUserModel>> Create(long friendId)
        {
            var ret = new ObjectRet<ChatUserModel>();
            var myFriend = await _chatFriendRep.GetByIdAsync(friendId);
            if (myFriend == null)
            {
                ret.Success = false;
                ret.Message = "请先添加好友";
                return ret;
            }
            using (var uow = _rep.Context.CreateContext(_rep.Context.Ado.IsNoTran()))
            {
                /**
                 *1.判断是否从聊天列表删除，双方都需要判断重新添加
                 *2.判断是否从聊天列表隐藏，只需将自己的改为显示
                 */
                //我是否将对方删除
                var myChatUser = await _rep.GetFirstAsync(x => x.MainId == myFriend.Id);
                if (myChatUser == null)
                {
                    //重新添加到聊天列表
                    myChatUser = new ChatUser
                    {
                        MainId = myFriend.Id,
                        UserId = _currentUser.UserId,
                        NickName = _currentUser.NickName,
                        TouserId = myFriend.FriendUserId,
                        ToNickName = myFriend.FriendNickName,
                        LastTime = DateTime.Now,
                        PairID = myFriend.PairID,
                        IsHide = false,
                    };
                    await myChatUser.InsertAsync();
                }
                else
                {
                    //判断我是否隐藏了聊天，如果从好友点击发送消息，这里将聊天改为显示
                    if (myChatUser.IsHide)
                    {
                        myChatUser.IsHide = !myChatUser.IsHide;
                        await _rep.UpdateAsync(myChatUser);
                    }
                }

                //对方是否将我从消息列表删除，如果是隐藏这里不需要改，需要在对方接收到消息时改为显示
                var friend = await _chatFriendRep.GetFirstAsync(x => x.UserId == myFriend.FriendUserId && x.FriendUserId == _currentUser.UserId);
                var friendChatUser = await _rep.GetFirstAsync(x => x.MainId == friend.Id);
                if (friendChatUser == null)
                {
                    friendChatUser = new ChatUser
                    {
                        MainId = friend.Id,
                        UserId = myFriend.FriendUserId,
                        NickName = myFriend.FriendNickName,
                        TouserId = _currentUser.UserId,
                        ToNickName = _currentUser.NickName,
                        LastTime = DateTime.Now,
                        PairID = friend.PairID,
                        IsHide = true,
                    };
                    await friendChatUser.InsertAsync();
                }

                ret.Success = uow.Commit();
                if (ret.Success)
                {
                    ret.Data = myChatUser.Adapt<ChatUserModel>();
                }
                return ret;
            }
        }
    }
}
